962F - Simple Cycles Edges - CodeForces Solution


dfs and similar graphs trees *2400

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
#define int long long
using namespace std;
#define min(a,b) (a<b?a:b)
const int N=5e5+5;
int dfn[N],low[N],m,n,tot,root,cnt;
stack<int>stk,stk2;
bool cut[N],vis[N];
vector<int>dcc[N];
vector<pair<int,int> >nbr[N];
#define fi first
#define se second
#define mp make_pair 
void tarjan(int cur,int ID){
	dfn[cur]=low[cur]=++tot;
	stk.push(cur);
	if(cur==root&&nbr[cur].empty()){
		dcc[++cnt].push_back(cur);
		return;
	}
	int flag=0;
	for(auto x:nbr[cur]){
		int to=x.fi,id=x.se;
		if(!dfn[to]){
			stk2.push(id);
			tarjan(to,id);
			low[cur]=min(low[cur],low[to]);
			if(low[to]>=dfn[cur]){
				++flag;
				if(root!=cur&&flag>1)cut[cur]=1;
				++cnt;
				int x=0,num=1,num2=0;
				while(!stk.empty()&&x!=to){
					x=stk.top();
					dcc[cnt].push_back(x);
					num++;
					stk.pop();
				}
				dcc[cnt].push_back(cur);
				x=0;
				queue<int>q;
				while(!stk2.empty()){
					x=stk2.top();
					q.push(x);
					stk2.pop();
					num2++;
					if(x==id)break;
				}
				if(num==num2){
					while(!q.empty()){
						vis[q.front()]=1;
						q.pop();
					}
				}
			}
		}
		else{
			if(dfn[cur]>dfn[to]&&id!=ID)stk2.push(id);
			low[cur]=min(low[cur],dfn[to]);
		}
	}
	return;
} 
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0); 
	cin>>n>>m;
	for(int i=1;i<=m;++i){
		int u,v;
		cin>>u>>v;
		if(u==v)continue;
		nbr[u].push_back(mp(v,i));
		nbr[v].push_back(mp(u,i));
	}
	for(int i=1;i<=n;++i)if(!dfn[i])root=i,tarjan(i,0);
	int sum=0;
	for(int i=1;i<=m;++i)if(vis[i])sum++;
	cout<<sum<<'\n';
	for(int i=1;i<=m;++i)if(vis[i])cout<<i<<' ';
	return 0;
}
 	 			 	  		  	   	 			 	 	 	 	


Comments

Submit
0 Comments
More Questions

1711B - Party
1702D - Not a Cheap String
1714F - Build a Tree and That Is It
1703F - Yet Another Problem About Pairs Satisfying an Inequality
610A - Pasha and Stick
1200A - Hotelier
1091A - New Year and the Christmas Ornament
1352B - Same Parity Summands
1102A - Integer Sequence Dividing
630B - Moore's Law
1004A - Sonya and Hotels
1680B - Robots
1690A - Print a Pedestal (Codeforces logo)
1295A - Display The Number
1077A - Frog Jumping
1714G - Path Prefixes
1369C - RationalLee
289B - Polo the Penguin and Matrix
1716A - 2-3 Moves
1670B - Dorms War
1716B - Permutation Chain
987A - Infinity Gauntlet
1676G - White-Black Balanced Subtrees
1716D - Chip Move
1352F - Binary String Reconstruction
1487B - Cat Cycle
1679C - Rooks Defenders
56A - Bar
1694B - Paranoid String
35A - Shell Game